﻿/*
 * BFind - A tool for finding football matches for trading on Betfair
 * Copyright 2009, 2010 Magnus Olsson <magnus72(at)gmail.com>
 * 
 * This file is part of BFind.
 * 
 * BFind is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BFind is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BFind.  If not, see <http://www.gnu.org/licenses/>
*/

using System;
using System.IO;


namespace betfairfind
{

    public class Logger
    {

         private string filename = @"log.txt";
         private string errorfilename = @"errorlog.txt";
         private string path = @"";
         static StreamWriter logstream;
         static StreamWriter errorlogstream;

        public static bool USE_CONSOLE = false;

        public Logger(string path, string filename)
        {
            if (!string.IsNullOrEmpty(filename))
            {
                this.filename = filename;
            }
            if (!string.IsNullOrEmpty(path))
            {
                this.path = path;
            }

    
            FileInfo file = new FileInfo(Path.Combine(this.path, this.filename));
            logstream = file.AppendText();
        }

        public Logger(string path, string filename, string errorlofilename): this(path, filename)
        {
            FileInfo errorfile = new FileInfo(Path.Combine(path, errorlofilename));
            errorlogstream = errorfile.AppendText();
        }

        static public void Log(string msg, bool file, bool cons)
        {
            if (file)
            {
                LogToFile(msg);
                
            }
              if (USE_CONSOLE && cons)
              {
//                  Console.WriteLine(DateTime.Now + ": " + msg);
                  Console.WriteLine( msg);
              }
        }

        static public void LogError(string msg, bool file, bool cons)
        {
            if (file)
            {
                LogToErrorFile(msg);

            }
            if (USE_CONSOLE && cons)
            {
                //                  Console.WriteLine(DateTime.Now + ": " + msg);
                Console.WriteLine(msg);
            }
        }

        static private void LogToFile(string msg)
        {
//            logstream.WriteLine(DateTime.Now +": " + msg);
            logstream.WriteLine(msg);
            logstream.Flush();
        }

        static private void LogToErrorFile(string msg)
        {
            //            logstream.WriteLine(DateTime.Now +": " + msg);
            errorlogstream.WriteLine(msg);
            errorlogstream.Flush();
        }

        static public void DebugToFile(string msg)
        {
            //            logstream.WriteLine(DateTime.Now +": " + msg);
            logstream.WriteLine("DEBUG: " + msg);
            logstream.Flush();
        }
    }
}
